package com.chis.netty.restful.server;

import com.chis.netty.utils.ServerSslContextFactory;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslHandler;

import javax.net.ssl.SSLEngine;

public class RestServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline ph = ch.pipeline();
        //添加ssl
        SSLEngine sslEngine = ServerSslContextFactory.getServerContext().createSSLEngine();
        sslEngine.setUseClientMode(false);
        ch.pipeline().addLast(new SslHandler(sslEngine));
        //处理http服务的关键handler
        //一个组合的HttpRequestDecoder和HttpResponseEncoder使得服务器端HTTP实现更容易。
        ph.addLast("codec", new HttpServerCodec());
        ph.addLast("aggregator", new HttpObjectAggregator(10*1024*1024));
        ph.addLast("handler", new RestServerHandler());// 服务端逻辑处理类
    }
}
